package com.chen.framework.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.framework.model.auth.AuthLinkUserRole;
import com.chen.framework.mapper.AuthLinkUserRoleMapper;
import com.chen.framework.service.inter.AuthPermissionService;
import com.chen.framework.service.inter.AuthLinkRolePermissionService;
import com.chen.framework.service.inter.AuthLinkUserRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: ~龙~
 * @date: 2023/6/13 17:15
 **/
@Service
@AllArgsConstructor
public class AuthLinkUserRoleServiceImpl extends ServiceImpl<AuthLinkUserRoleMapper, AuthLinkUserRole> implements AuthLinkUserRoleService {

    private final AuthLinkRolePermissionService authLinkRolePermissionService;
    private final AuthPermissionService authPermissionService;

    @Override
    public List<String> authoritiesListByUserId(Long userId) {
        Set<Long> roleIdSet = list(
                Wrappers.lambdaQuery(AuthLinkUserRole.class).eq(AuthLinkUserRole::getUserId, userId)
        ).stream().map(AuthLinkUserRole::getRoleId).collect(Collectors.toSet());

        Set<Long> permissionIdSet = authLinkRolePermissionService.getPermissionIdSet(roleIdSet);
        Set<String> permissionCodeSet = authPermissionService.getPermissionCodeSet(permissionIdSet);
        return new ArrayList<>(permissionCodeSet);
    }
}
